package com.second.controller.pay;


import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.second.config.AliPayConfig;
import com.second.mapper.FGoodsMapper;
import com.second.model.pojo.FGoods;
import com.second.model.pojo.FOrd;
import com.second.moudle.Result;
import com.second.service.IFOrdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("pay")
public class PayController {
    @Autowired
    private IFOrdService ordService;

    @Autowired
    private FGoodsMapper goodsMapper;

    @GetMapping()
    public String payIndex(){
        return "jsp/pay/pay";
    }


    //    添加订单
    @PostMapping("/createOrd/{ordPrice}/{userId}/{shopId}/{goodsId}")
    @ResponseBody
    public Result createOrd(@PathVariable("ordPrice") Double ordPrice,
                            @PathVariable("userId") Integer userId,
                            @PathVariable("shopId") Integer shopId,
                            @PathVariable("goodsId") Integer goodsId
    ){

//        System.out.println(ordService.save(ordPrice,userId));


        return new Result("200","success",ordService.save(ordPrice,userId,shopId,goodsId));
    }
//    根据订单号查询订单
    @RequestMapping("/find/{orderCode}")
    public ModelAndView findByOrdCode(
            @PathVariable("orderCode") String orderCode
    ){
        ModelAndView modelAndView = new ModelAndView();
        FOrd fOrd=ordService.findByOrdCode(orderCode);
        FGoods goods = goodsMapper.findById(fOrd.getGoodsId()).get();
        modelAndView.addObject("fOrd",fOrd);
        modelAndView.addObject("goods",goods);
        modelAndView.addObject("orderCode",orderCode);
        modelAndView.setViewName("jsp/pay/pay");
        return modelAndView;
    }
}
